package com.gxwzxy.medic.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxwzxy.medic.entity.Chronicdis;
import com.gxwzxy.medic.service.ChronicdisService;
import com.gxwzxy.medic.service.Impl.ChronicdisServiceImpl;
import com.gxwzxy.medic.until.PageBean;
import com.my.web.servlet.RequestBeanUtils;
import com.mysql.fabric.Response;

/**
 * 这是处理慢性病分类管理的类
 * Servlet implementation class ChronicdisServlet
 */
public class ChronicdisServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 慢性病分类分页展示
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findChronicdis(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// 获取响应数据
		String sindex = request.getParameter("index");
		int index = 1;
		try {
			index = Integer.parseInt(sindex);
		} catch (NumberFormatException n) {
			n.printStackTrace();
		}

		PageBean<Chronicdis> pu = new PageBean<Chronicdis>();
		pu.setIndex(index);// 设置展示的当前页数
		ChronicdisService cs = new ChronicdisServiceImpl();
		cs.pageChronicdis(pu);
		request.setAttribute("pu", pu);
		request.getRequestDispatcher("page/chronicdis/chronicdis.jsp").forward(request, response);
	}
	
	/**
	 * 保存慢病
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void saveIll(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//获取响应数据
		Chronicdis c = RequestBeanUtils.requestToBean(request, Chronicdis.class);
		//处理请求
		ChronicdisService cs = new ChronicdisServiceImpl();
		if(cs.saveIll(c)) {
			request.setAttribute("url", "ChronicdisServlet?method=findChronicdis&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	
	/**
	 * 根据illCode修改慢病
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void updateChr(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//获取响应数据
		Chronicdis c = RequestBeanUtils.requestToBean(request, Chronicdis.class);
		//处理请求
		ChronicdisService cs = new ChronicdisServiceImpl();
		if(cs.updateChr(c)) {
			request.setAttribute("url", "ChronicdisServlet?method=findChronicdis&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	/**
	 * 根据illCode删除慢病
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void deleteIll(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//获取响应数据
		String illCode = request.getParameter("illCode");
		//处理请求
		ChronicdisService cs = new ChronicdisServiceImpl();
		if(cs.deleteIll(illCode)) {
			response.getWriter().write("true");
		}else {
			response.getWriter().write("false");
		}
	}
}
